package com.szholly.data.coderule.rules;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.szholly.utils.util.StringUtils;
import com.szholly.data.coderule.table.*;
import com.szholly.data.general.*;

/// <summary>
/// 定义序列值编码规则
/// </summary>
@Component("CodeRule_9")
@Scope("prototype")
class SequenceValueRule extends CodeRuleBase {
	// / <summary>
	// / 获取代码规则值
	// / </summary>
	// / <param name="table">具体表</param>
	// / <param name="entity">要编码的实体</param>
	// / <param name="para">规则生成参数</param>
	// / <returns></returns>
	@Override
	public String GetCodeRule(ITable table, RowBase entity, String para,
			RowBase refEntity, String curCodeRule, String ruleInfoID) {
		if (StringUtils.IsNullOrSpace(para))
			return "";
		String[] paras = para.split("\\|");
		if (paras == null || paras.length != 2)
			return "";

		RuleValueRow rvRow = RuleValueTable.GetRowByID(ruleInfoID);
		int nowValue = -1;
		int stepValue = -1;
		if (rvRow != null) {
			nowValue = Integer.parseInt(rvRow.getRULEVALUE());
		} else {
			nowValue = Integer.parseInt(paras[0]);
		}

		stepValue = Integer.parseInt(paras[1]);

		if (nowValue < 0 || stepValue < 0)
			return "";
		if (rvRow != null)
			nowValue = nowValue + stepValue;

		if (rvRow != null) {
			rvRow.setRULEVALUE("" + nowValue);
		} else {
			rvRow = new RuleValueRow();
			ruleInfoID = ruleInfoID.substring(1, ruleInfoID.length() - 2);
			rvRow.setRULEINFOID(ruleInfoID.toString());
			rvRow.setRULEVALUE("" + nowValue);
			if (table != null)
				rvRow.setTABLEID(table.getTableName());
		}
		RuleValueTable.getSingleRef().save(rvRow);
		return "" + nowValue;
	}

	// / <summary>
	// / 获取当前是否需要编码规则
	// / </summary>
	// / <param name="table">具体表</param>
	// / <param name="entity">要编码的实体</param>
	// / <param name="para">规则生成参数</param>
	// / <param name="refEntity">参考对象</param>
	// / <param name="fields">字段名集合</param>
	// / <returns></returns>
	@Override
	public Boolean NeedCodeRule(ITable table, RowBase entity, String para,
			RowBase refEntity, String fields) {
		return false;
	}
}
